const router = require('koa-router')();
router.prefix('/promoter/promoter');

const {
    numberFromCtx,
    stringFromCtx,
    formatErrorJson,
    formatSuccessJson
} = require('../../../../utils/retUtil');
const CONST = require('../../../../utils/const');
const api4back = require('../../../../service/api4back');




router.get('/', async (ctx, next) => {
    await ctx.render('index', {
        title: 'Hello Koa 2! by yjx/v1/promoter/promoter'
    })
});

/**
 * 获取推广码列表
 * @author 李强
 * @date 2018-07-3
 *
 * 参数：pageNum : 请求页码
 * 参数：pageSize : 请求页容量
 * 参数：withTotalPage : 是否需要总页数
 * 参数：cType : 检索条件：码类型（10直接抵扣,20满减券,30折扣券,40活动推广码）
 * 参数：userId : 检索条件：用户id（只返回绑定到该用户的码）
 * 参数：desc : 检索条件：码备注（模糊匹配）
 * 参数：state : 码的状态
 */
const getPromoCodeList = async (ctx, next) =>{
    //获取参数
    const pageNum = numberFromCtx(ctx, 'pageNum') || 0;
    const pageSize = numberFromCtx(ctx, 'pageSize') || 10;
    const withTotalPage = 1;
    const cType = stringFromCtx(ctx, 'cType') || "[10,20,30,40]";
    let userId = 0;
    let desc = stringFromCtx(ctx, 'desc') || "";
    const state = stringFromCtx(ctx, 'state') || "[20,40]";

    if(!desc || desc.length === 0){
        desc = "";
    }

    //参数判断
    if(ctx.session[CONST.SESSION_USER]
        && ctx.session[CONST.SESSION_USER].userInfo
        && ctx.session[CONST.SESSION_USER].userInfo.userId){
        userId = ctx.session[CONST.SESSION_USER].userInfo.userId;
    }else{
        throw new Error("权限不足");
    }

    //数据请求
    const para = {
            pageNum:pageNum,
            pageSize:pageSize,
            withTotalPage:withTotalPage,
            userId:userId
        };

    if(cType.length){
        para.cType = cType;
    }
    if(state.length){
        para.state = state;
    }
    if(desc.length){
        para.desc = desc;
    }
    const data = await api4back.getPromoCodeList(para);
    data.pageSize = pageSize;
    if(data.list.length>0){
        for(let i = 0;i<data.list.length;i++){
            if(data.list[i].c_type === 10){
                data.list[i].c_type_desc = "直接抵扣券";
            }else if(data.list[i].c_type === 20){
                data.list[i].c_type_desc = "满减券";
            }else if(data.list[i].c_type === 30){
                data.list[i].c_type_desc = "折扣券";
            }else if(data.list[i].c_type === 40){
                data.list[i].c_type_desc = "活动推广码";
            }
            data.list[i].key = data.list[i].c_id;
        }
    }
    ctx.body = formatSuccessJson(data);
};
router.get('/getPromoCodeList', getPromoCodeList);
router.post('/getPromoCodeList', getPromoCodeList);




/**
 * 生成优惠码/推广码
 * @author 李强
 * @date 2018-07-3
 *
 * 参数：cType : 码类型（10直接抵扣,20满减券,30折扣券,40活动推广码）
 * 参数：userId : 用户id（只返回绑定到该用户的码）
 * 参数：desc :码备注
 */
const createPromoCode = async (ctx, next) =>{
    //获取参数
    const cType = numberFromCtx(ctx, 'cType01') || 40;
    let userId = 0;
    const desc = numberFromCtx(ctx, 'desc01') || "";

    //参数判断
    //参数判断
    if(ctx.session[CONST.SESSION_USER]
        && ctx.session[CONST.SESSION_USER].userInfo
        && ctx.session[CONST.SESSION_USER].userInfo.userId){
        userId = ctx.session[CONST.SESSION_USER].userInfo.userId;
    }else{
        throw new Error('权限不足');
    }

    //数据请求
    const para = {
            cType:cType,
            desc:desc,
            userId:userId
        };
    ctx.body = formatSuccessJson(await api4back.createPromoCode(para));
};
router.get('/createPromoCode', createPromoCode);
router.post('/createPromoCode', createPromoCode);



/**
 * 获取推广码奖励信息列表
 * @author 李强
 * @date 2018-07-3
 *
 * 参数：pageNum : 请求页码
 * 参数：pageSize : 请求页容量
 * 参数：withTotalPage : 是否需要总页数
 * 参数：userId : 检索标间：码绑定用户的id
 * 参数：desc : 检索条件：码备注（模糊匹配）
 * 参数：actId : 检索条件：活动id
 * 参数：cCode : 检索条件：推广码
 * 参数：cId : 检索条件：码id
 */
const getPromoCodeRewardList = async (ctx, next) =>{
    //获取参数
    const pageNum = numberFromCtx(ctx, 'pageNum') || 0;
    const pageSize = numberFromCtx(ctx, 'pageSize') || 10;
    const withTotalPage = 1;
    const actId = numberFromCtx(ctx, 'actId') || 0;
    let cCode = stringFromCtx(ctx, 'cCode') || "";
    const cId = numberFromCtx(ctx, 'cId') || 0;
    let desc = stringFromCtx(ctx, 'desc') || "";
    let userId = 0;

    if(!desc || desc.length === 0){
        desc = "";
    }

    if(!cCode || cCode.length === 0 ){
        cCode = "";
    }

    //参数判断
    if(ctx.session[CONST.SESSION_USER]
        && ctx.session[CONST.SESSION_USER].userInfo
        && ctx.session[CONST.SESSION_USER].userInfo.userId){
        userId = ctx.session[CONST.SESSION_USER].userInfo.userId;
    }else{
        throw new Error("权限不足");
    }

    //数据请求
    const para = {
            pageNum:pageNum,
            pageSize:pageSize,
            withTotalPage:withTotalPage,
            userId:userId
        };

    if(actId){
        para.body.actId = actId;
    }
    if(cCode.length){
        para.body.cCode = cCode;
    }
    if(cId){
        para.body.cId = cId;
    }
    if(desc.length){
        para.body.desc = desc;
    }
    const data = api4back.getPromoCodeRewardList(para);
    data.pageSize = pageSize;
    if(data.list.length>0){
        for(let i = 0;i<data.list.length;i++){
            data.list[i].key = i*1+1;
            data.list[i].p_reward = (data.list[i].p_reward/100).toFixed(2)+"元";
        }
    }
    ctx.body = formatSuccessJson(data);
};
router.get('/getPromoCodeRewardList', getPromoCodeRewardList);
router.post('/getPromoCodeRewardList', getPromoCodeRewardList);




module.exports = router;
